// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:netease_common_ui/common_ui.dart';
import 'package:netease_common_ui/utils/color_utils.dart';
import 'package:netease_corekit_im/im_kit_client.dart';
import 'package:netease_corekit_im/router/imkit_router.dart';
import 'package:netease_corekit_im/router/imkit_router_constants.dart';
import 'package:nim_chatkit_ui/chat_kit_client.dart';
import 'package:nim_contactkit_ui/contact_kit_client.dart';
import 'package:nim_conversationkit_ui/conversation_kit_client.dart';
import 'package:nim_searchkit_ui/search_kit_client.dart';
import 'package:nim_teamkit_ui/team_kit_client.dart';

import 'src/home/splash_page.dart';
import 'src/mine/user_info_page.dart';
import 'src/nim_initial.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarColor: Colors.transparent));
  WidgetsFlutterBinding.ensureInitialized().addPostFrameCallback((timeStamp) {
    //初始化位置消息插件
    NimInitial.instance.initGeoLocation();
  });
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({Key? key}) : super(key: key);

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  // Uint8List? _deviceToken;

  @override
  void initState() {
    super.initState();
    NimInitial.instance.updateTokenIOS();
    _initPlugins();
    GestureBinding.instance.resamplingEnabled = true;
  }

  ///init all plugin here
  void _initPlugins() {
    // ChatKitClient.init();
    // TeamKitClient.init();
    // ConversationKitClient.init();
    // ContactKitClient.init();
    // SearchKitClient.init();

    NimInitial.instance.initPlugins();
    IMKitRouter.instance.registerRouter(RouterConstants.PATH_MINE_INFO_PAGE, (context) => const UserInfoPage());
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      useInheritedMediaQuery: true,
      builder: (context, child) {
        return MaterialApp(
          onGenerateTitle: (BuildContext context) => "云信IM",
          localizationsDelegates: [
            CommonUILocalizations.delegate,
            ConversationKitClient.delegate,
            ChatKitClient.delegate,
            ContactKitClient.delegate,
            TeamKitClient.delegate,
            SearchKitClient.delegate,
            ...GlobalMaterialLocalizations.delegates,
          ],
          navigatorObservers: [IMKitRouter.instance.routeObserver],
          supportedLocales: IMKitClient.supportedLocales,
          theme: ThemeData(
              primaryColor: CommonColors.color_337eff,
              pageTransitionsTheme: const PageTransitionsTheme(builders: {
                TargetPlatform.android: CupertinoPageTransitionsBuilder(),
                TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
              }),
              appBarTheme: const AppBarTheme(
                  backgroundColor: Colors.white,
                  elevation: 1,
                  iconTheme: IconThemeData(color: CommonColors.color_333333),
                  titleTextStyle: TextStyle(fontSize: 16, color: CommonColors.color_333333),
                  systemOverlayStyle: SystemUiOverlayStyle.dark),
              useMaterial3: false),
          routes: IMKitRouter.instance.routes,
          home: child,
        );
      },
      child: SplashPage(deviceToken: NimInitial.instance.deviceToken),
    );
  }
}
